package com.javasm.service;

import com.javasm.entity.Users;
import com.javasm.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author: Lisa
 * @className: UserService
 * @description:
 * @date: 2023/10/21 19:01
 * @VERSION: 0.1
 * @SINCE: jdk11
 */
@Service
public class UserService implements UserDetailsService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        List<Users> users = userMapper.findUserByUsername(s);
        if (CollectionUtils.isEmpty(users)) return null;
        return users.get(0);
    }


    public Users findUserByEmail(String email){
        List<Users> users = userMapper.findUserByEmail(email);
        if (CollectionUtils.isEmpty(users)) return null;
        return users.get(0);
    }

}
